package com.agregatesdk.recommender;

import java.io.InputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.adview.adwall.AdViewOffer;
import com.agregatesdk.core.Utils;

public class AdviewRecommender {
    private static Dialog dialog;

    private static InputStream bgStream = AdviewRecommender.class.getClassLoader().getResourceAsStream("assets/common_bg.png");
    private static InputStream confirmStream = AdviewRecommender.class.getClassLoader().getResourceAsStream("assets/button.png");
    private static InputStream closedBtnStream = AdviewRecommender.class.getClassLoader().getResourceAsStream("assets/jifenqiang_del_pic.png");
    private static Drawable bgDrawable = Drawable.createFromStream(bgStream, "");
    public static Drawable confirmDrawable = Drawable.createFromStream(confirmStream, "");
    private static Drawable closeDrawable = Drawable.createFromStream(closedBtnStream, "");

    public static AdViewOffer adViewOffer;

    public static void jifenqiangChecker(final Activity context) {

        SharedPreferences config = context.getSharedPreferences("gtx_jfq", 0);
        int usedTimes = config.getInt("usedTimes", 0);

        if ((usedTimes + 1) % 5 == 0) {
            adViewOffer = new AdViewOffer(context, "SDK20132130090409zbbqv8umczpztnh");
            
            showAlert(context);

        }

        config.edit().putInt("usedTimes", usedTimes + 1).commit();
    }

    private static void showAlert(final Activity context) {
    	DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        int confirmID = 0;
        int descId = 1;
        int cancalid = 2;

        RelativeLayout mainpage = new RelativeLayout(context);
        RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        mainpage.setLayoutParams(p);
        mainpage.setBackgroundDrawable(bgDrawable);

        RelativeLayout.LayoutParams contentP = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        contentP.addRule(RelativeLayout.CENTER_HORIZONTAL);
        contentP.topMargin = Utils.convertToScreenPixels(100, displayMetrics.density);
        StringBuilder description = new StringBuilder();
        description.append("更多热门应用等着您哦！");

        TextView content = new TextView(context);
        content.setId(descId);
        content.setGravity(Gravity.CENTER);
        content.setLayoutParams(p);
        content.setTextSize(15);
        content.setTextColor(Color.parseColor("#3333CC"));
        content.setTypeface(Typeface.DEFAULT_BOLD);
        content.setText(Html.fromHtml(description.toString()));
        content.setLayoutParams(contentP);

        RelativeLayout.LayoutParams p1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        p1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        p1.addRule(RelativeLayout.BELOW, descId);
        p1.topMargin = Utils.convertToScreenPixels(30, displayMetrics.density);;
        Button confirmButton = new Button(context);
        confirmButton.setId(confirmID);
        confirmButton.setLayoutParams(p1);
        confirmButton.setBackgroundDrawable(confirmDrawable);
        confirmButton.setText("去逛逛");
        confirmButton.setTextColor(Color.WHITE);

        RelativeLayout.LayoutParams p2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        p2.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
        p2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);

        ImageButton cancelButton = new ImageButton(context);
        cancelButton.setId(cancalid);
        cancelButton.setLayoutParams(p2);
        cancelButton.setBackgroundDrawable(closeDrawable);

        mainpage.addView(content);
        mainpage.addView(confirmButton);
        mainpage.addView(cancelButton);

        dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);  
        
        dialog.setContentView(mainpage, new LayoutParams(-1, Utils.convertToScreenPixels(250,  displayMetrics.density)));

        confirmButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                adViewOffer.showOffer(context);

            }
        });

        cancelButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }

            }
        });

        dialog.show();
    }

    public static int convertToScreenPixels(int dipPixels, double density) {
        return (int) convertToScreenPixels((double) dipPixels, density);
    }

    public static double convertToScreenPixels(double dipPixels, double density) {
        return density > 0.0D ? dipPixels * density : dipPixels;
    }
}
